<?php
/**
 * Simple Light PHP Framework
 *
 * @copyright Marc PEPIN 2012
 * @author Marc PEPIN
 *
 */

defined('SLP_ROOT') or die("You've not access to this page");


/**
 * Simple Light Php Framework Slp Class
 *
 * @package             Slp
 * @author              Marc PEPIN
 */

class Slp {
	
	/**
	* @var Slp Slp instance
	*/
	
	static public $instance = NULL;
	
	
	/**
	* @var object Loaded classes
	*/
	
	public $loaded;
	
	
	/**
	* Constructor
	*/
	
	public function __construct() {
		$this->loaded = new stdClass;
		self::$instance = $this;
	}
	
	
	/**
    * Get an instance of Slp
    * @return Slp
   	*/
	
	static public function getInstance() {
		if (!self::$instance)
			self::$instance = new Slp;
		return self::$instance;
	}
	
	
	/**
    * load class inside slp class
	* @param string $class class to load
	* @param array
	* @parram bool
	* @param bool
    */
	
	public function loadClass($class, $args = NULL, $load = true) {
		if (isset($this->loaded->$class))
			return $this->loaded->$class;
		$param = !is_array($args) ? array($args) : $args;
		$p = explode('_', $class);
		$path = isset($p[1]) ? $p[1] : $p[0];
		if (is_file(SLP_ROOT.'/system/libraries/'.$p[1].'.php'))
           	require SLP_ROOT.'/system/libraries/'.$p[1].'.php';
		else if (is_file(SLP_ROOT.'/application/libraries/'.$p[1].'.php'))
           	include SLP_ROOT.'/application/libraries/'.$p[1].'.php';
		if (!$load)
			return;
		$r = new ReflectionClass($class);
		/**
		* @todo add exception
		*/
   		$this->loaded->$class  = ($args ? $r->newInstanceArgs($param) : $r->newInstance());
		return $this->loaded->$class;
	}
	
}

	
?>